package com.easylinkin.linkappapi.common.utils.location;

/**
 * Created with IntelliJ IDEA.
 *
 * @Author: 胡明威
 * @Date: 2023/10/23/11:36
 * @Description: 根据信号强度（rssi）值三点定位
 */
public class ThreePointLocationUtil {

//    private static final double[] accessPointLocationsX = {0.3300, 0.3563, 0.5275}; // 三个无线接入点的x坐标
//    private static final double[] accessPointLocationsY = {0.3270, 0.7028, 0.3885}; // 三个无线接入点的y坐标
//    private static final double[] rssiValues = {-40, -80, -90}; // 三个无线接入点的 RSSI 值
//
//    public static void main(String[] args) {
//        double estimatedX = estimatePosition(accessPointLocationsX, rssiValues);
//        double estimatedY = estimatePosition(accessPointLocationsY, rssiValues);
//        System.out.println("Estimated position: (" + estimatedX + ", " + estimatedY + ")");
//    }

    public static double estimatePosition(double[] accessPointLocations, double[] rssiValues) {
        double x = 0;
        double y = 0;
        for (int i = 0; i < accessPointLocations.length; i++) {
            double weight = calculateWeight(accessPointLocations[i], rssiValues[i]);
            x += accessPointLocations[i] * weight;
            y += weight;
        }
        return x / y;
    }

    private static double calculateWeight(double accessPointLocation, double rssiValue) {
        // 这里简单地使用了距离的倒数作为权重，实际应用中可能需要更复杂的算法来计算权重。
        return 1 / Math.pow(-30 - rssiValue, 2);
    }
}
